package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.eva.androidlibrary.api.entity.ShopInfo;
import com.eva.baidulibrary.BDLocationClient;
import com.eva.baidulibrary.BDMapClient;
import com.eva.baidulibrary.BaiduZoomLevel;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseTitleActivity;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 店铺定位页面
 *
 * @author eva
 * @date 16/6/27
 */
@EActivity(R.layout.activity_shop_location)
public class ShopLocationActivity extends BaseTitleActivity {

    @ViewById(R.id.shop_location_map_view)
    MapView mMapView;

    private String mLatitude, mLongitude, mAddress;
    private BaiduMap mBaiduMap;
    private BDLocationClient mBDLocationClient = BDLocationClient.getInstance();
    private BDMapClient mMapClient = BDMapClient.getInstance();

    public static void jumpToShopLocation(Context context) {
        context.startActivity(new Intent(context, ShopLocationActivity_.class));
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.shop_location_title);
    }

    @Override
    protected void initializeUI() {
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        // 开启定位图层
        mMapClient.openMyLocation(mBaiduMap, 0);
        mBDLocationClient.requestLocation(this, new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation != null) {
                    mLatitude = Double.toString(bdLocation.getLatitude());
                    mLongitude = Double.toString(bdLocation.getLongitude());
                    mAddress = bdLocation.getAddrStr();
                    // 显示当前位置
                    mMapClient.showMyLocation(mBaiduMap, bdLocation, BaiduZoomLevel.LEVEL_200_METER);
                }
            }
        });

    }

    @Click(R.id.shop_location_ok_button)
    void clickOk() {
        ShopInfo shop = new ShopInfo();
        shop.setLatitude(mLatitude);
        shop.setLongitude(mLongitude);
        shop.setAddress(mAddress);
        AddOrEditShopActivity.jumpToAddOrEditShop(this, shop);
        this.finish();
    }
}
